package com.example.wtt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.wtt.common.Result;
import com.example.wtt.entity.Validation;
import com.example.wtt.service.ValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/validation")
public class ValidationController {
    @Autowired
    private ValidationService validationService;
    // 查询接口
    @GetMapping
    public Result findAll(){
        return Result.success(validationService.list());
    }

    //增、改接口
    @PostMapping
    public Result save(@RequestBody Validation validation){
        return Result.success(validationService.saveOrUpdate(validation));
    } // 将前端传来的json转换为后端的java对象

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(validationService.removeById(id));
    }

    // 批量删除
    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(validationService.removeBatchByIds(ids));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                           @RequestParam String email){
        IPage<Validation> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Validation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(!"".equals(email),Validation::getEmail, email);
        queryWrapper.orderByDesc("id");
        return Result.success(validationService.page(page, queryWrapper));

    }
}
